#include"common.hpp"
using std::cout;
using std::cerr;
using std::endl;

int main()
{
    key_t key=ftok("PATH_NAME",PROJ_ID);
    cout<<"Start creat share memory!"<<endl;

    int shmid=shmget(key,SHM_SIZE,IPC_CREAT);
    if(shmid==-1)
    {
        cerr<<"shmget error!"<<endl;
        exit(1);
    }
    cout<<"Share memory had creat success!shmid::"<<shmid<<"  key::"<<key<<endl;
    
    sleep(2);
    char* str=(char*)shmat(shmid,nullptr,0);
    if(str==(void*)-1)
    {
        cerr<<"shmat error!"<<endl;
        exit(2);
    }
    cout<<"Shmat success!"<<endl;

    while(true)
    {
        cout<<str<<endl;
        sleep(1);
    }

    int dtret=shmdt(str);
    if(dtret==-1)
    {
        cerr<<"Dettach fail!"<<endl;
        exit(2);
    }
    cout<<"Dettach success!"<<endl;

    int ret=shmctl(shmid,IPC_RMID,nullptr);
    if(ret!=-1)
    {
        cerr<<"shmctl error!"<<endl;
        exit(2);
    }
    cout<<"Shmctl success!"<<endl;
    return 0;
}
